//
//  RCServerImpressCell.m
//  RocketProject
//
//  Created by chen jiedan on 13-9-28.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCServerImpressCell.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "TTTAttributedLabel.h"
#import "NSAttributedString+LVAddition.h"

#define kBGImageFile            @"image_discount_cell_bg.png"

@implementation RCServerImpressCell{
    UILabel *_nameLabel;
    UILabel *_dateTime;
    TTTAttributedLabel *_content;
    UIImageView *horizonLine;
    
    UIButton *_failBtn;
    UIButton *_passBtn;
    int64_t  _reqID;

}


@synthesize horizonLine = horizonLine;
@synthesize impressiveID = _impressiveID;
@synthesize delegate = _delegate;
@synthesize reviewStatus = _reviewStatus;

- (void) dealloc
{
    _delegate =  nil;
    KF_RELEASE_SAFELY(_impressiveID);
    [super dealloc];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        
        UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
        [self setBackgroundColorWithImage:bgImage];
        self.backgroundColor = [UIColor clearColor];
        
        [self createPlaceHolder];

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void) createPlaceHolder
{
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 23, 65, 12)];
    [self.contentView addSubview:_nameLabel];
    _nameLabel.font = [UIFont systemFontOfSize:12];
    _nameLabel.textColor = [UIColor colorWithHex:0xffff9333];
    [_nameLabel setBackgroundColor:[UIColor clearColor]];
    [_nameLabel release];
    
    
    _dateTime = [[UILabel alloc] initWithFrame:CGRectMake(85, 23, 160, 12)];
    [self.contentView addSubview:_dateTime];
    _dateTime.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    _dateTime.textColor = [UIColor colorWithHex:0xff656565];
    [_dateTime setBackgroundColor:[UIColor clearColor]];
    [_dateTime release];
    
    _content = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(15, 45, 260, 12)];
    [self.contentView addSubview:_content];
    _content.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _content.textColor = [UIColor colorWithHex:0xff989797];
    _content.numberOfLines = 0;
    _content.leading = 6;
    [_content setBackgroundColor:[UIColor clearColor]];
    [_content release];
    
    
    _failBtn = [[UIButton alloc] initWithFrame:CGRectMake(275, 0, 40, 40)];
    [_failBtn setImage:[UIImage imageNamed:@"icon_comment_fail"] forState:UIControlStateNormal];
    [_failBtn setImage:[UIImage imageNamed:@"icon_comment_fail"] forState:UIControlStateHighlighted];
    
    [_failBtn addTarget:self action:@selector(failBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_failBtn];
    [_failBtn release];
    
    
    _passBtn = [[UIButton alloc] initWithFrame:CGRectMake(275, 40, 40, 40)];
    [_passBtn setImage:[UIImage imageNamed:@"icon_comment_pass"] forState:UIControlStateNormal];
    [_passBtn setImage:[UIImage imageNamed:@"icon_comment_pass"] forState:UIControlStateHighlighted];
    [_passBtn addTarget:self action:@selector(passBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_passBtn];
    [_passBtn release];
    
    
    
    horizonLine = [[UIImageView alloc]initWithFrame:CGRectMake(0, 80, 320, 1)];
    horizonLine.backgroundColor = [UIColor clearColor];
    [horizonLine setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_line_type3.png" ofType:nil]]];
    [self.contentView addSubview:horizonLine];
    [horizonLine release];
    
}


- (void) bindData:(RCImpressiveItemDataModel *)item
{
    _nameLabel.text = item.userName;
    _dateTime.text  = item.datetime;
    _content.text   = item.content;
    CGSize size = [_content.attributedText sizeWithConstrainedToSize:CGSizeMake(260, 2000) Leading:6];
    CGRect rect = _content.frame;
    rect.size.height = size.height;
    _content.frame = rect;

    CGRect horFrame = horizonLine.frame;
    horFrame.origin.y  = _content.frame.origin.y + _content.frame.size.height +23;
    horizonLine.frame = horFrame;

    self.impressiveID = item.commentID;
    
    CGFloat height = horizonLine.frame.origin.y;
    
    if (self.reviewStatus == RCListReviewStatus_UnderReview) {
        _passBtn.hidden = NO;
        _failBtn.hidden = NO;
        
        _failBtn.frame = CGRectMake(275, 0, 40, height/2);
        _passBtn.frame = CGRectMake(275, height/2, 40, height/2);
        
    }else if (self.reviewStatus == RCListReviewStatus_ReviewSuccess){
        _passBtn.hidden = YES;
        _failBtn.hidden = NO;
        _failBtn.frame = CGRectMake(275, (height-40)/2, 40, 40);
    }else if (self.reviewStatus == RCListReviewStatus_ReviewFailed){
        _passBtn.hidden = NO;
        _failBtn.hidden = YES;
        _passBtn.frame = CGRectMake(275, (height-40)/2, 40, 40);
    }
    
}


+ (CGFloat) cellHeight:(RCImpressiveItemDataModel *)item
{
    CGFloat height = 0;
    RCServerImpressCell *cell = [[RCServerImpressCell alloc] init];
    [cell bindData:item];
    height = cell.horizonLine.frame.origin.y + cell.horizonLine.frame.size.height;
    [cell release];
    return height;
}


- (void) failBtnClick
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    [[IManager sharedInstance] requestUpdateImpressive:self.impressiveID withState:ImpressiveState_Fail withDelegate:self];

}

- (void) passBtnClick
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    [[IManager sharedInstance] requestUpdateImpressive:self.impressiveID withState:ImpressiveState_Pass withDelegate:self];
}


- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if ([_delegate respondsToSelector:@selector(didStateChange:withMsg:)]) {
        [_delegate didStateChange:NO withMsg:@"提交失败"];
    }
}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if (dataModel.errorNO == RCErrorTypeNoError) {
        if ([_delegate respondsToSelector:@selector(didStateChange:withMsg:)]) {
            [_delegate didStateChange:YES withMsg:nil];
        }
    }else{
        if ([_delegate respondsToSelector:@selector(didStateChange:withMsg:)]) {
            [_delegate didStateChange:NO withMsg:dataModel.errorMsg];
        }
    }
}
@end
